import grace from "../../utils/grace.js"
grace.page({
      data: {
            tips: [],
            amount: 300,
            default_amount: [300, 500, 1000, 2000, 3000, 5000]
      },
      onLoad: function(options) {
            this.loadData();
      },
      loadData: function() {
            this.$http.get('api/order/user_recharge_discount_list', {})
                  .then((res) => {
                        this.$data.tips = res.list;
                  });
      },
      chooseHandle: function(e) {
            this.$data.amount = e.target.dataset.amount;
      },
      inputHandle: function(e) {
            this.$data.amount = e.detail.value;
      },
      doCharge: function() {
            let amount = this.$data.amount;
            if (parseFloat(amount) <= 0) {
                  this.toast('充值金额必须要大于0!');
                  return;
            }
            if (isNaN(parseFloat(amount))) {
                  this.toast('请填写有效的数字!');
                  return;
            }
            wx.showToast({
                  title: '提交中..',
                  icon: 'loading',
                  duration: 2000
            })
            let that = this;
            this.$http.get('api/order/user_recharge_pay', {
                  amount: amount
            }).then((res) => {
                  let data = JSON.parse(res.data);
                  wx.requestPayment({
                        'timeStamp': data.timeStamp,
                        'nonceStr': data.nonceStr,
                        'package': data.package,
                        'signType': data.signType,
                        'paySign': data.paySign,
                        'success': function(res) {
                              that.toast('充值成功！');
                              that.$goBack({
                                    isUpdate: 1
                              });
                        },
                        'fail': function(res) {
                              if (res.errMsg != 'requestPayment:fail cancel') {
                                    that.toast('支付失败');
                              }
                        }
                  })


            })
      }
})